import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Leetcode1455 {

    public int isPrefixOfWord(String sentence, String searchWord) {
        String[] array = sentence.split(" ");
        for (int i = 0; i < array.length; i++) {
            if (match(array[i], searchWord)) {
                return i + 1;
            }
        }


        return -1;
    }

    private boolean match(String word1, String word2) {
        if (word1.length() < word2.length()) {
            return false;
        }

        char[] origin = word1.toCharArray();
        char[] match = word2.toCharArray();
        for (int i = 0; i < match.length; i++) {
            if (origin[i] != match[i]) {
                return false;
            }
        }

        return true;
    }


    public int isPrefixOfWord1(String sentence, String searchWord) {
        String[] wordArray = sentence.split(" ");
        return IntStream.range(0, wordArray.length).map(i -> wordArray[i].startsWith(searchWord) ? i+1 : -1).filter(i -> i != -1).findFirst().orElse(-1);
    }
}
